package com.fg.cloud.common;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.joda.time.DateTime;

/**
 * @author ：
 * @date ： 2017年6月22日 下午1:33:36
 * @Desc ：单据编号生成工具类
 */
public class BillNoUtils {

	private static int sn = 0;
	private static Lock lock = new ReentrantLock();// 锁对象

	private BillNoUtils() {
	}

	public static String getBillNo() {
		lock.lock();// 得到锁
		try {
			if (sn == 999)
				sn = 0;
			else
				sn++;
			return DateTime.now().toString("yyyyMMddHHmmssSSS") + padLeft(Integer.toString(sn),3,'0');
		} finally {
			lock.unlock();// 释放锁
		}
	}

	public static String padLeft(String src, int len, char ch) {
		int diff = len - src.length();
		if (diff <= 0) {
			return src;
		}
		char[] charr = new char[len];
		System.arraycopy(src.toCharArray(), 0, charr, 0, src.length());
		for (int i = src.length(); i < len; i++) {
			charr[i] = ch;
		}
		return new String(charr);
	}
}
